<template>
  <div class="main">
    <!-- 文章列表table表单 -->
    <SkuDetail ref="SkuDetail" class="array" />
    <el-card shadow="never" class="card">
      <div slot="header" class="clearfix">
        <el-button type="primary" size="default" @click="add"
          >新增文章</el-button
        >
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
        max-height="1000"
        v-loading="loading"
        border
      >
        <el-table-column label="#" fixed type="index" width="50">
        </el-table-column>
        <el-table-column
          prop="addressName"
          label="地址名称"
          width="200"
          heigth="133"
          align="center"
        >
        </el-table-column>
        <el-table-column label="默认发货地址" width="150">
          <template slot-scope="scope">
            默认发货地址：
            <el-switch
              v-model="scope.row.sendStatus"
              active-color="#409EFF"
              inactive-color="#DCEFE6"
              :active-value="1"
              :inactive-value="0"
              @change="changeStatused(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="收货人姓名"
          width="150"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="phone"
          label="收货人电话"
          width="150"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="phone"
          label="收货人地址"
          width="200"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.province }}
            {{ scope.row.city }}
            {{ scope.row.region }}
            {{ scope.row.detailAddress }}
          </template>
        </el-table-column>
        <el-table-column label="默认收货地址" width="150">
          <template slot-scope="scope">
            默认收货地址：
            <el-switch
              v-model="scope.row.receiveStatus"
              active-color="#409EFF"
              inactive-color="#DCEFE6"
              :active-value="1"
              :inactive-value="0"
              @change="changeStatus(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="200"
          align="center"
        >
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              @click="edit(scope.row)"
              type="text"
              size="small"
              style="color: aqua !important"
            >
              编辑
            </el-button>
            <el-button
              @click="deleteRow(scope.row)"
              type="text"
              size="small"
              style="color: red !important"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import orderFor from '@/api/orderFor.js'
import SkuDetail from './components/SkuDetail.vue'
export default {
  components: {
    SkuDetail,
  },
  data() {
    return {
      searchFrom: {}, //输入框里的对象
      form: {}, //实际搜索要用的表单
      tableData: [], //文章列表数据
      loading: false,

      value: '',
    }
  },
  created() {
    this.addressList()
  },
  mounted() {},
  methods: {
    //编辑地址
    edit(row) {
      console.log(row, '编辑')
      this.$refs.SkuDetail.open(row)
    },
    //新增文章add
    add() {
      console.log('新增')
      this.$refs.SkuDetail.open()
    },

    //删除地址
    deleteRow(row) {
      console.log(row, '删除')
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        orderFor.companyAddressApi(row.id).then(res => {
          if (res.success) {
            this.$message.success('删除成功')
            this.addressList()
          } else {
            this.$message.error(res.message)
          }
        })
      })
    },

    //获取地址列表
    addressList() {
      orderFor.addressListApi().then(res => {
        console.log(res, 'res')
        if (res.success) {
          this.tableData = res.data.items //获取到线上文字地址
        } else {
          this.$message.error('请求失败')
        }
        console.log(this.tableData, 'this.tableData')
      })
    },
    //修改发货地址
    changeStatus(row) {
      //暂存一个改变之前的状态
      console.log('修改')
      var ermpStatus = row.isShow === 1 ? 0 : 1 //如果失败了  0就是1  1就是0
      orderFor
        .setReceiveOneApi({
          isShow: row.isShow,
          id: row.id,
        })
        .then(res => {
          console.log(res, 'changeShowApi.res')
          if (res.success) {
            this.$message.success('修改状态成功')
          } else {
            this.$message.error('修改状态失败，请联系管理员')
            row.isShow = ermpStatus
          }
        })
    },
    //修改收货地址
    changeStatused(row) {
      //暂存一个改变之前的状态

      this.tableData.forEach(item => {
        item.sendStatus = 0
      })
      console.log('修改', this.tableData)
      row.sendStatus = 1
      // var ermpStatus = row.isShow === 1 ? 0 : 1 //如果失败了  0就是1  1就是0
      orderFor
        .setSendOneApi({
          isShow: row.isShow,
          id: row.id,
        })
        .then(res => {
          console.log(res, 'changeShowApi.res')
          if (res.success) {
            this.$message.success('修改状态成功')
          } else {
            this.$message.error('修改状态失败，请联系管理员')
            row.isShow = ermpStatus
          }
        })
    },
  },
}
</script>

<style lang="scss" scoped>
.main {
  background: #f5f5f5;
  .card {
    width: 96.5%;
    margin: 30px;
  }
}
.condtion {
  background-color: #fff;
  color: #303133;
  font-size: 10px;
}
.users {
  margin-right: 200px;
}
.usered {
  position: relative;
  top: 30px;
}
.el-card__body {
  height: 1200px;
}
.frame {
  color: #409eff;
  border-radius: 4px;
  background-color: #ecf5ff;
  border-color: #d9ecff;
}
</style>